package com.medical.controller;

import com.medical.constant.MessageConstant;
import com.medical.constant.RedisConstant;
import com.medical.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile) throws IOException {
        try{
            String originalFilename = imgFile.getOriginalFilename();
            int last = originalFilename.indexOf(".");
            String subffix = originalFilename.substring(last - 1);
            String fileName = UUID.randomUUID().toString() + subffix;

            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }
}
